<template>
	<z-paging ref="paging" :paging-style="{background: '#f7f7f7'}">
		<!-- 头部 -->
		<template slot="top">
			<u-navbar title="修改手机号" autoBack bgColor="#fff" :fixed="false"></u-navbar>
		</template>

		<view class="form-box">
			<u--form :model="model1" ref="uForm" labelWidth="50">
				<u-form-item label="手机号" prop="userInfo.name" borderBottom>
					<u--input v-model="model1.userInfo.phone" type="number" border="none" inputAlign="right"
						placeholder="请输入新手机号"></u--input>
				</u-form-item>
				<u-form-item label="验证码" prop="userInfo.name">
					<u-input v-model="model1.userInfo.code" type="number" maxlength="6" border="none" inputAlign="right"
						placeholder="请输入内容">
						<template slot="suffix">
							<u-code ref="uCode" @change="codeChange" seconds="60" changeText="XS后重新获取"></u-code>
							<view class="code-desc" @tap="getCode">{{tips}}</view>
						</template>
					</u-input>
				</u-form-item>
			</u--form>
		</view>
		<view class="submit" @click="submit">提交</view>
	</z-paging>
</template>

<script>
	export default {
		data() {
			return {
				model1: {
					userInfo: {
						phone: '',
						code: '',
					},
				},
				tips: '',
			}
		},
		onReady() {

		},
		methods: {
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				if (this.$refs.uCode.canGetCode) {
					let {
						phone
					} = this.model1.userInfo
					if (!uni.$u.test.mobile(phone)) {
						uni.showToast({
							title: '请输入正确手机号',
							icon: 'error'
						});
						return;
					}
					uni.showLoading({
						mask: true
					});

					this.$Api.wxPhoneSendSms({
						phone
					}).then((res) => {
						console.log(res, '获取验证码');
						uni.hideLoading();
						if (res.code == 200) {
							// 这里此提示会被this.start()方法中的提示覆盖
							uni.$u.toast('验证码已发送');
							// 通知验证码组件内部开始倒计时
							this.$refs.uCode.start();
						}
					}).catch((err) => {
						uni.hideLoading();
					});
				}
			},

			submit() {
				let {
					phone,
					code
				} = this.model1.userInfo

				if (!uni.$u.test.mobile(phone)) {
					this.$refs.uToast.show({
						message: "请输入正确手机号",
					})
					return;
				}
				if (uni.$u.test.isEmpty(code)) {
					this.$refs.uToast.show({
						message: "请输入验证码",
					})
					return;
				}
				uni.showLoading({
					mask: true
				});

				this.$hApi.wxUpdatePhone({
					phone,
					code,
				}).then(res => {
					console.log(res);
					uni.hideLoading();
					if (res.code == 200) {
						let userInfo = uni.getStorageSync('userInfo')
						userInfo.phone = phone
						uni.setStorageSync('userInfo', userInfo);
						uni.navigateBack({
							delta: 1
						});
					}
				})
			},

		}
	}
</script>

<style lang="scss" scoped>
	.form-box {
		background-color: #fff;
		padding: 0 40rpx;
		margin-top: 2rpx;
	}

	.code-desc {
		padding-left: 20rpx;
		margin-left: 20rpx;
		border-left: 2rpx solid #D3D7DB;
		color: #3B75FF;
	}

	.submit {
		margin: 134rpx 24rpx 0;
		background: #3B75FF;
		border-radius: 16rpx;
		color: #fff;
		font-size: 28rpx;
		height: 88rpx;
		line-height: 88rpx;
		text-align: center;
	}
</style>